package com.fantasy.Compared;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import com.fantasy.excel.ReadExcel;
import com.fantasy.file.GetFolderAbsPath;
import com.fantasy.file.WriteTxt;
import com.fantasy.readproperties.ReadProperties;
import com.fantasy.string.StringUtils;

public class ComparedDLML {

	private static String path = GetFolderAbsPath.getPath();

	public static void main(String[] args) {
		//System.out.println(path);

		Properties properties = ReadProperties.read("ComparedDLML.properties");
		String hkEmailFile = path + properties.getProperty("hkEMailFileName");
		String tsEmailFile = path + properties.getProperty("tsEMailFileName");
		Map<String, List<String>> hkHashMap = new HashMap<String, List<String>>();
		Map<String, List<String>> tsHashMap = new HashMap<String, List<String>>();
		Map<String, String> hkMlNameMap = new HashMap<String, String>();
		Map<String, String> tsMlNameMap = new HashMap<String, String>();
		Set<String> mlKey = new HashSet<>();
		Workbook hkWorkbook = ReadExcel.getWorkbook(new File(hkEmailFile));
		Sheet hkSheet = hkWorkbook.getSheetAt(0);
		//System.out.println("" + hkSheet.getSheetName());
		String hkMLName = "";
		for (int i = 0; i < hkSheet.getLastRowNum(); i++) {
			if (i > -1) {
				if(hkSheet.getRow(i) == null){
					continue;
				}
				String colValue = ReadExcel.getCellFormatValue(hkSheet.getRow(i).getCell(0));
				//System.out.println(colValue);
				if (colValue.startsWith("#")) {
					int inx = colValue.lastIndexOf("@");
					if(inx > -1){
						colValue = colValue.substring(0, inx);
					}
					String colValueKey = StringUtils.reMoveSpecialChar(colValue.replace("# distributionList", "").replace(" ", "")
							.toUpperCase());
					//System.out.println(colValueKey);
					List<String> hkMLUserList = new ArrayList<>();
					hkHashMap.put(colValueKey, hkMLUserList);
					hkMLName = colValueKey;
					hkMlNameMap.put(colValueKey, colValue);
					mlKey.add(colValueKey);
				} else if (colValue.trim().startsWith("*") || "members".equals(colValue.trim())
						|| colValue.startsWith("-") || "PrimarySmtpAddress".equals(colValue.trim()) ) {

				} else {
					//System.out.println(hkMLName);
					hkHashMap.get(hkMLName).add(getFormat(colValue.trim()));
				}
			}
		}

		Workbook tsWorkbook = ReadExcel.getWorkbook(new File(tsEmailFile));
		Sheet tsSheet = tsWorkbook.getSheetAt(0);
		String tsMLName = "";
		for (int i = 0; i < tsSheet.getLastRowNum(); i++) {
			if (i > -1) {
				if(tsSheet.getRow(i) == null){
					continue;
				}
				String colValue = ReadExcel.getCellFormatValue(tsSheet.getRow(i).getCell(0));
				//System.out.println(colValue);
				if (colValue.startsWith("#")) {
					int inx = colValue.lastIndexOf("@");
					String colValueKey = StringUtils.reMoveSpecialChar(colValue.substring(0, inx).replace("# distributionList", "").replace(" ", "")
							.toUpperCase());
					//System.out.println(colValueKey);
					List<String> tsMLUserList = new ArrayList<>();
					tsHashMap.put(colValueKey, tsMLUserList);
					tsMLName = colValueKey;
					tsMlNameMap.put(colValueKey, colValue);
					mlKey.add(colValueKey);
				} else if (colValue.trim().startsWith("*") || "members".equals(colValue.trim())) {

				} else {
					tsHashMap.get(tsMLName).add(getFormat(colValue.trim()));
				}
			}
		}
		WriteTxt.open(path + "comparedResult.txt");
		WriteTxt.write("在TS不在HK\r\n");
		for (String key : tsHashMap.keySet()) {
			if(!hkHashMap.containsKey(key)){
				mlKey.remove(key);
				WriteTxt.write(tsMlNameMap.get(key) + "\r\n");
			}
		}
		WriteTxt.write("在HK不在TS\r\n");
		for (String key : hkHashMap.keySet()) {
			if(!tsHashMap.containsKey(key)){
				mlKey.remove(key);
				WriteTxt.write(hkMlNameMap.get(key) + "\r\n");
			}
		}
		WriteTxt.write("****************************************************************\r\n\r\n");
		WriteTxt.write("TS与HK的不同\r\n\r\n");
		WriteTxt.write("---------------------------------------------------------------\r\n");
		for (String key : mlKey) {
			List<String> hkList = new ArrayList<String>();
			hkList.addAll(hkHashMap.get(key));
			hkList.removeAll(tsHashMap.get(key));
			List<String> tsList = new ArrayList<String>();
			tsList.addAll(tsHashMap.get(key));
			tsList.removeAll(hkHashMap.get(key));
			
			if(hkList.size() > 0 || tsList.size() > 0){
				WriteTxt.write(hkMlNameMap.get(key) + "\r\n");
				WriteTxt.write(tsMlNameMap.get(key) + "\r\n");
			}
			
			if(hkList.size() > 0){
				WriteTxt.write("hk多了\r\n");
			}
			for (String string : hkList) {
				WriteTxt.write(string + "\r\n");
			}
			if(tsList.size() > 0){
				WriteTxt.write("ts多了\r\n");
			}
			for (String string : tsList) {
				WriteTxt.write(string + "\r\n");
			}
			if(hkList.size() > 0 || tsList.size() > 0){
				WriteTxt.write("---------------------------------------------------------------\r\n\r\n");
			}
		}
		
		WriteTxt.close();
	}
	
	public static String getFormat(String str ){
		int inx = str.lastIndexOf("@");
//		System.out.println("---------------------------------");
//		System.out.println(str);
//		System.out.println(inx);
//		System.out.println("-------------------------------------");
		if(inx > 0){
			str = str.substring(0, inx);
		}
		return str.trim().toLowerCase();
	}
}
